package com.example.mall.dto.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * tokenPassword检验器
 * - 实现 ConstraintValidator接口
 */
public class TokenPassowrdValidator implements ConstraintValidator<TokenPassword, String> {
    private int min;
    private int max;

    @Override
    public void initialize(TokenPassword constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        // 自定义-检验逻辑
        // password可以为空 - 第三方授权.
        if ("".equals(s)) {
            return true;
        }
        boolean res = s.length() >= min && s.length() <= max ? true : false;
        System.out.println("密码长度检验:" + res);
        return res;
    }
}
